﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using BTechBids.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace BTechBids.Data
{
    public class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool seedUsers = true, bool seedCategories = true, bool seedItems = true, bool seedImages = true)
        {
            List<ApplicationUser> UserList = new List<ApplicationUser>();

            var admin = new ApplicationUser()
            {
                FirstName = "BTech",
                LastName = "Bids",
                UserName = "Admin",
                Email = "btechbids@gmail.com",
                Wallet = 50.00,
                BidCount = 50
            };
            //var user01 = new ApplicationUser()
            //{
            //    FirstName = "User",
            //    LastName = "One",
            //    UserName = "user01",
            //    Email = "user01@mail.com",
            //    Wallet = 20.00,
            //    BidCount = 20
            //};
            //var user02 = new ApplicationUser()
            //{
            //    FirstName = "User",
            //    LastName = "Two",
            //    UserName = "user02",
            //    Email = "user02@mail.com",
            //    Wallet = 20.00,
            //    BidCount = 20
            //};
            UserList.Add(admin);
            //UserList.Add(user01);
            //UserList.Add(user02);

            if (seedUsers)
            {
                var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
                roleManager.Create(new IdentityRole { Name = "Admin" });
                UserManager<ApplicationUser> um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

                foreach (var user in UserList)
                {
                    if (um.FindByName(user.UserName) == null)
                    {
                        var result = um.Create(user, "pass01");
                    }
                }
                db.SaveChanges();
                var au = db.Users.Where(x => x.UserName == "Admin").FirstOrDefault();
                um.AddToRole(au.Id, "Admin");
            }
            //user01 = db.Users.Where(x => x.UserName == user01.UserName).FirstOrDefault();
            //user02 = db.Users.Where(x => x.UserName == user02.UserName).FirstOrDefault();

            if (seedCategories)
            {
                db.Categories.AddOrUpdate(
                    c => c.Name,
                    new Category("Smartwatches"),
                    new Category("Smartphones"),
                    new Category("Computers/Laptops"),
                    new Category("Artificial Intelligence/Robots"),
                    new Category("Futuristic Technology"),
                    new Category("Flying Things")
                    );
                db.SaveChanges();
            }

            if (seedItems)
            {
                db.Items.AddOrUpdate(
                    i => i.Name,
                    new Item(1, "Asus Zenwatch", "ASUS ZenWatch is the first ASUS wearable device powered by Android Wear and produced in partnership with Google. ASUS ZenWatch is an exquisitely crafted watch that seamlessly pairs with an Android smartphone to extend its functionality. And the design and style is impeccable.") { msRemaining = 30000 },
                    new Item(2, "iPhone 6", "iPhone 6 isn’t simply bigger — it’s better in every way. Larger, yet dramatically thinner. More powerful, but remarkably power efficient. With a smooth metal surface that seamlessly meets the new Retina HD display.") { msRemaining = 360000 },
                    new Item(3, "Bento Book", "The Bento Box is a laptop that acts as a kind of hub for your smartphone, tablet, and external hard drive. It features several shallow depressions to store and sync your 11-inch tablet, smartphone, and SSD drive. The battery is a solar powered lithium-ion. Though this laptop/hub for all your multimedia devices is incredibly smart, its design requires compatible devices. ") { msRemaining = 240000 },
                    new Item(4, "Autonomous Robots with a License to Kill", "Own an autonomous killing machines could potentially invade this world. It comes with an upgraded Aegis Combat System that operates in an autonomous mode.") { msRemaining = 480000 },
                    new Item(5, "Lightsaber", "Thought lightsabers were fake? Well they're not. Here's a blue lightsaber that can slice through anything in its way.") { msRemaining = 600000 },
                    new Item(6, "Audi FX (Flying Car)", "The first transitional flying car made by Audi has recently made its first flight.") { msRemaining = 720000 }
                    );
                db.SaveChanges();

            }

            if (seedImages)
            {
                db.Images.AddOrUpdate(
                    i => i.Url,
                    new ImageUrl(1, "http://www.oled-info.com/files/Asus-ZenWatch.jpg"),
                    new ImageUrl(1, "http://cdn.ndtv.com/tech/images/gadgets/asus_zenwatch.jpg"),
                    new ImageUrl(2, "http://cdn2.macworld.co.uk/cmsdata/features/3414914/Apple-iPhone-6-iPhone-6s-official.jpg"),
                    new ImageUrl(3, "http://www5.pcmag.com/media/images/258882-bento-book.jpg"),
                    new ImageUrl(4, "http://blogs.telegraph.co.uk/news/files/2013/11/terminator.jpg"),
                    new ImageUrl(4, "http://img.gawkerassets.com/img/18sxhfjbsztngjpg/ku-xlarge.jpg"),
                    new ImageUrl(5, "http://images2.fanpop.com/images/polls/126187_1223506335116_full.jpg"),
                    new ImageUrl(5, "http://dwnloadwallpapers.com/wp-content/uploads/2014/05/Light-Saber.jpg"),
                    new ImageUrl(6, "http://psipunk.com/wp-content/uploads/2008/12/flying_car_audi_calamaro_concept.jpg"),
                    new ImageUrl(6, "http://fictigristle.files.wordpress.com/2013/09/flying_car_audi_calamaro_concept.jpg")
                    );
                db.SaveChanges();
            }
        }
    }
}
